#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using FolderData;

#endregion

namespace Folder
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class StoryScreen : GameScreen
    {
        enum ScreenStatus
        {
            Init,
            playing,
            End,
        }

        #region Fields

        Texture2D tex_bg, tex_story, tex_selected_small;

        ScreenStatus status;
        int lineCount;
        float lineAlpha;
        String storyAssetName;

        #endregion

        List<String> stringList = new List<string>();
        int storyNumber;


        public StoryScreen(String assetName, int storyNumber)
        {
            status = ScreenStatus.playing;
            storyAssetName = assetName;
            lineCount = 0;
            lineAlpha = 0.0f;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            IsPopup = true;
            this.storyNumber = storyNumber;
        }


        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;
            
            tex_bg = content.Load<Texture2D>("Menu/background");
            tex_selected_small = content.Load<Texture2D>("Menu/content_selected_small");

            Story story = content.Load<Story>(storyAssetName);
            this.stringList = story.StringList;
            tex_story = content.Load<Texture2D>(story.BgAssetName);

            base.LoadContent();
        }

        public override void HandleInput()
        {
            if (status == ScreenStatus.End && InputManager.IsActionTriggered(InputManager.Action.Ok))
            {
                ExitScreen();
                if(storyNumber != -1)
                    ScreenManager.AddScreen(new ContentsScreen(storyNumber+1));
            }

            base.HandleInput();
        }

        #region Update and Draw

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            switch (status)
            {
                case ScreenStatus.playing:
                    lineAlpha += 0.015f;
                    if (lineAlpha > 1.0f)
                    {
                        ++lineCount;
                        lineAlpha = 0.0f;
                        if (lineCount == stringList.Count)
                            status = ScreenStatus.End;
                    }
                    break;
                case ScreenStatus.End:
                    break;
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        private static readonly Vector2 pos = new Vector2(220, 60);
        private static readonly int spacing = 32;

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            spriteBatch.Draw(tex_bg, Vector2.Zero, TransitionColor);
            spriteBatch.Draw(tex_story, Vector2.Zero, TransitionColor);

            switch (status)
            {
                case ScreenStatus.playing:
                    for (int i = 0; i < lineCount; ++i)
                        spriteBatch.DrawString(Fonts.StoryFont, stringList[i], pos + new Vector2(0, spacing * i), InverseTransitionColor);

                    spriteBatch.DrawString(Fonts.StoryFont, stringList[lineCount], pos + new Vector2(0, spacing * lineCount), new Color(0, 0, 0, lineAlpha));

                    break;
                case ScreenStatus.End:
                    for (int i = 0; i < stringList.Count; ++i)
                        spriteBatch.DrawString(Fonts.StoryFont, stringList[i], pos + new Vector2(0, spacing * i), InverseTransitionColor);

                    Vector2 Position = new Vector2(900, 650);
                    spriteBatch.DrawString(Fonts.ContentFont, "Next", Position, InverseTransitionColor);
                    spriteBatch.Draw(tex_selected_small, Position+new Vector2(-5, 20), TransitionColor);

                    break;

            }

            spriteBatch.End();
        }
        #endregion
    }
}
